package controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import bean.Advertisement;
import bean.CartItem;

import connection.ServiceProxy;

/**
 * Servlet implementation class Checkout
 */
public class Checkout extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Checkout() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String destination = null;
		
		String message = null,result = "false"; 
		HttpSession session = request.getSession();
		String userId = (String)session.getAttribute("userId");
		ServiceProxy oProxy = new ServiceProxy();
		
		
			result = oProxy.checkAvailableQuantityForItemsInCart(userId);
			
			if(result.equals("true")){
				destination = "/checkout.jsp";
				CartItem aoCartItem[] = oProxy.getUserCart(userId);
				request.setAttribute("CartItem", aoCartItem);
				
				Advertisement aoAdvert[] = oProxy.getUserCartItemDetails(userId);
				request.setAttribute("Advertisement", aoAdvert);
				
			}
			else if(result.equals("false")) {
				message = "Available Quantity is less than Ordered Quantity";
				destination = "/ViewCart";
			}
			
			
			RequestDispatcher rd = this.getServletContext().getRequestDispatcher(destination);
			rd.forward(request, response);
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		String destination = null;
		
		String message = null,result = "false"; 
		HttpSession session = request.getSession();
		String userId = (String)session.getAttribute("userId");
		ServiceProxy oProxy = new ServiceProxy();
		
		result = oProxy.checkAvailableQuantityForItemsInCart(userId);
			
			if(result.equals("true")){
				destination = "/checkout.jsp";
				CartItem aoCartItem[] = oProxy.getUserCart(userId);
				request.setAttribute("CartItem", aoCartItem);
				
				Advertisement aoAdvert[] = oProxy.getUserCartItemDetails(userId);
				request.setAttribute("Advertisement", aoAdvert);
				
			}
			else if(result.equals("false")) {
				message = "Available Quantity is less than Ordered Quantity";
				destination = "/ViewCart";
			}
			RequestDispatcher rd = this.getServletContext().getRequestDispatcher(destination);
			rd.forward(request, response);
		
		
	}

}
